package com.mianle.userapi.service;

import ch.hsr.geohash.GeoHash;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.mianle.userapi.mapper.GaodeMapper;
import com.mianle.userapi.mapper.ResHandle;
import com.mianle.userapi.mapper.SessionMapper;
import com.mianle.userapi.model.Gaode;
import com.mianle.userapi.util.JsonResponseData;
import com.mianle.userapi.util.JsonResponseEnum;
import com.mianle.userapi.util.LbsUtil;
import com.spatial4j.core.context.SpatialContext;
import com.spatial4j.core.distance.DistanceUtils;
import com.spatial4j.core.shape.Rectangle;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yiyuan
 * @Description:
 * @Date: Created in 8:55 2018/5/21
 */
@Service
public class GaodeService {

    @Autowired
    private GaodeMapper gaodeMapper;

    @Autowired
    private SessionMapper sessionMapper;

    public List<Double[]> getAll() {

        Gaode gaode = new Gaode();
        List<Gaode> list = (List<Gaode>) gaodeMapper.selectList(gaode);
        List<Double[]> result = Lists.newArrayList();
        for (Gaode g : list) {
            Double[] r = new Double[2];
            r[0] = g.getLongitude();
            r[1] = g.getLatitude();
            result.add(r);
        }

        return result;
    }



    /**
     * Mysql不使用空间函数
     *
     * @param lng 经度
     * @param lat 纬度
     * @return
     */
    public List<Gaode> getMarker(Double lng, Double lat) {
        //lng 经度  lat 纬度
        //112.881042,28.228963 中电软件园
        //x为纬度，y为经度
        lng = 112.881042;
        lat = 28.228963;

        ////里面的 1 就代表搜索 1km 之内，单位km
        double range = 180 / Math.PI * 1 / 6372.797;
        double lngR = range / Math.cos(lat * Math.PI / 180.0);
        double maxLat = lat + range;
        double minLat = lat - range;
        double maxLng = lng + lngR;
        double minLng = lng - lngR;
        //minLat、maxLat、minLng、maxLng

        // 千米
        int radius = 1;
        SpatialContext geo = SpatialContext.GEO;
        Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(
                geo.makePoint(lng, lat), radius * DistanceUtils.KM_TO_DEG, geo, null);
        List<Gaode> list = gaodeMapper.getMarker(rectangle.getMinY(), rectangle.getMaxY(),  rectangle.getMinX(), rectangle.getMaxX());

        //List<Gaode> list = gaodeMapper.getMarker(minLat, maxLat,  minLng, maxLng);

        return list;
    }

    /**
     *  Mysql使用空间函数
     * @param lng
     * @param lat
     * @return
     */
    public JsonResponseData getSpatialMarker(Double lng, Double lat) {
        lng = 112.880114;
        lat = 28.229365;
        //GeoHash 有边界问题,利用geohash-java, 获取其相邻的8个矩形
        List<String> paramList = LbsUtil .getAdjacent(lng, lat, 6);
        Page<Map<String, Object>> page = gaodeMapper.getSpatialMarker(lat, lng, paramList);
        PageInfo<Map<String, Object>> userList = new PageInfo<>(page);
        return JsonResponseEnum.SUCCESS.result(userList);
    }

    public JsonResponseData getAlls() {
        Page<Map<String, Object>> page = gaodeMapper.getAll();
        PageInfo<Map<String, Object>> userList = new PageInfo<>(page);
        return JsonResponseEnum.SUCCESS.result(userList);
    }

    public int save() {
        Gaode gao = new Gaode();
        gao.setAddrName("demo");
        gao.setLatitude(65.5656);
        gao.setLongitude(89.25656);
        return gaodeMapper.save(gao);
    }



    public static void main(String[] args) {
        //x为纬度，y为经度
        double lng = 112.881042;
        double lat = 28.228963;

        //
        ////里面的 1 就代表搜索 1km 之内，单位km
        double range = 180 / Math.PI * 1 / 6372.797;
        double lngR = range / Math.cos(lat * Math.PI / 180.0);
        double maxLat = lat + range;
        double minLat = lat - range;
        double maxLng = lng + lngR;
        double minLng = lng - lngR;
        //minLat、maxLat、minLng、maxLng
        System.out.println(minLat+"-"+maxLat);
        System.out.println(minLng+"-"+maxLng);

        //spatial4j
        // 千米
        int radius = 1;
        SpatialContext geo = SpatialContext.GEO;
        Rectangle rectangle = geo.getDistCalc().calcBoxByDistFromPt(
                geo.makePoint(lng, lat), radius * DistanceUtils.KM_TO_DEG, geo, null);
        // 经度范围
        System.out.println(rectangle.getMinX() + "-" + rectangle.getMaxX());
        // 纬度范围
        System.out.println(rectangle.getMinY() + "-" + rectangle.getMaxY());

        //距离
//        double distance = geo.calcDistance(geo.makePoint(lon1, lat1), geo.makePoint(lon2, lat2))
//                * DistanceUtils.DEG_TO_KM;
//        System.out.println(distance);// KM

        //112.878729,28.229315
        System.out.println("-------------------------------------------------------------------------------------------");
        GeoHash geoHash = GeoHash.withCharacterPrecision(28.229315, 112.878729, 6);
        //获取当前的GeoHash的值
        System.out.println("当前 "+ geoHash.toBase32());
        //获取整个九宫格的GeoHash的值
        GeoHash[] adjacent = geoHash.getAdjacent();
        for (GeoHash hash : adjacent) {
            System.out.println(hash.toBase32());
        }
    }

    public void demo() {
        Map<String, Object> maps = new HashMap<String, Object>();
        maps.put("geohash", "wt028e");


        Map<String, Object> map =  sessionMapper.selectMap(maps);
        System.out.println(map);
    }
}
